import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter_base/base/widget/base_get_builder.dart';
import 'package:flutter_base/config/app_color.dart';
import 'package:flutter_base/generated/assets.dart';
import 'package:flutter_base/widgets/image/auto_image.dart';
import 'package:flutter_base/widgets/keep_alive_widget.dart';
import 'main_logic.dart';
import '../home/home_view.dart';
import '../order/order_view.dart';
import '../mine/mine_view.dart';

class MainPage extends StatelessWidget {
  final logic = Get.put(MainLogic());

  MainPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainLogic>(
      init: logic,
      builder: (_) {
        return Scaffold(
          body: PageView(
            controller: logic.pageController,
            physics: const NeverScrollableScrollPhysics(),
            children: [
              // 首页
              KeepAliveWidget(
                child: HomePage(),
              ),

              // Container(),
              // Container(),
              // 订单页
              KeepAliveWidget(
                child: OrderPage(),
              ),
              // 我的页面
              KeepAliveWidget(
                child: MinePage(),
              ),
            ],
          ),
          bottomNavigationBar: _buildBottomNavigationBar(),
        );
      },
    );
  }


  // 构建底部导航栏
  Widget _buildBottomNavigationBar() {
    return Container(
      height: kBottomNavigationBarHeight + ScreenUtil().bottomBarHeight,
      padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          top: BorderSide(
            color: AppColor.colorF5F7F5,
            width: 1.w,
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          _buildNavItem(0, Assets.navHomeNor, Assets.navHomeSel),
          _buildNavItem(1, Assets.navOrderNor, Assets.navOrderSel),
          _buildNavItem(2, Assets.navMineNor, Assets.navMineSel),
        ],
      ),
    );
  }

  // 构建导航项
  Widget _buildNavItem(int index, String icon, String activeIcon) {
    bool isSelected = logic.currentIndex == index;
    return Expanded(
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () => logic.switchTab(index),
        child: Center(
          child: AutoImage(
            isSelected ? activeIcon : icon,
            width: 24.w,
            height: 24.w,
          ),
        ),
      ),
    );
  }
}
